<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Moto Marca Modelo
 *
 * @package		cms
 * @author		Lucas Volgarini
 */
class Moto_modelo_dados_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->config('parametros');
    }

    /**
     * @access	public
     * @return	array
     */
    function getAllAnoModelosOrderByAno() {
        $this->db->order_by("ano ASC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModeloDados'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	integer
     * @return	array
     */
    function getAllAnoModelosByModelo($_idModelo) {
        $this->db->where('moto_modelo_id', $_idModelo);
        $this->db->order_by("ano ASC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModeloDados'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	integer
     * @return	array
     */
    function getAnoModelo($_ano, $_modeloId) {
        $this->db->where('moto_modelo_id', $_modeloId);
        $this->db->where('ano', $_ano);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModeloDados'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function getAnoModeloFull($_modeloId) {
        $this->db->select('mmd.moto_modelo_id, mmd.ano, mmd.combustivel, mmd.codigo_fipe, mmd.preco_medio, mm.marca_id, mm.estilo_id, mm.nome as modelo, mm.cilindrada, mma.nome as marca, me.nome as estilo');

        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblModelo') . ' as mm', 'mmd.moto_modelo_id = mm.id', 'INNER');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblMarca') . ' as mma', 'mm.marca_id = mma.id', 'INNER');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblEstilo') . ' as me', 'mm.estilo_id = me.id', 'LEFT OUTER');

        $this->db->where('mmd.id', $_modeloId);

        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModeloDados') . ' as mmd');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	array
     * @return	void
     */
    function inserir($_dados) {
        $this->db->set($_dados);
        $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblModeloDados'));
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return NULL;
        }
    }

    /**
     * @access	public
     * @param	integer
     * @param   array
     * @return	boolean
     */
    function atualizar($_id, $_dados) {
        $this->db->where('id', $_id);
        $this->db->set($_dados);
        $this->db->update($this->config->item('dbCliente') . $this->config->item('tblModeloDados'));
        if ($this->db->affected_rows() > 0) {
            return $_id;
        } else {
            return NULL;
        }
    }

    /**
     * @access	public
     * @return	array
     */
    function getAllAnoModelosOrderById() {
        $this->db->order_by("id DESC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModeloDados'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function getAnoModeloById($_id) {
        $this->db->where('id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModeloDados'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function combo($_idModelo) {
        $result = $this->getAllAnoModelosByModelo($_idModelo);

        if ($result != null) {
            $option = array();
            $option[''] = 'Selecionar';
            foreach ($result as $value) {
                $option[$value->id] = $value->ano;
            }
            return $option;
        }
        return null;
    }

    public function comboValor($_idModelo) {
        $result = $this->getAllAnoModelosByModelo($_idModelo);

        if ($result != null) {
            $option = array();
            $option[''] = 'Selecionar';
            foreach ($result as $value) {
                $option[$value->ano] = $value->ano;
            }
            return $option;
        }
        return null;
    }

}